package _01_动态数组;

public class _70_爬楼梯 {

    public static void main(String[] args) {


        System.out.println(climbStairs(2));
    }

    // 遍历优化
    public static int climbStairs(int n) {
        if (n <= 1) return n;
        int first = 1;
        int second = 2;
        for (int i = 3; i <= n; i++) {
            second = first + second;
            first = second - first;
        }
        return second;
    }

    // 动态规划
    public static int climbStairs2(int n) {
        if (n <= 1) return n;
        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        for (int i = 3; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }

    // 递归，效率太低
    public static int climbStairs1(int n) {
        if (n <= 2) return n;
        return climbStairs(n - 1) + climbStairs(n - 2);
    }

}
